package cn.itsource.basic.service.impl;

import cn.itsource.basic.mapper.BaseMapper;
import cn.itsource.basic.query.BaseQuery;
import cn.itsource.basic.service.IBaseService;
import cn.itsource.basic.util.PageBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.UUID;


@Transactional(propagation = Propagation.SUPPORTS , readOnly = true)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    BaseMapper<T> mapper;

    @Transactional
    @Override
    public boolean add(T t) {
        return mapper.add(t)>0;
    }

    @Transactional
    @Override
    public boolean update(T t) {
        return mapper.update(t)>0;
    }

    @Transactional
    @Override
    public boolean delete(Long id) {
        return mapper.delete(id)>0;
    }

    @Override
    public T queryById(Long id) {
        return mapper.queryById(id);
    }

    @Override
    public List<T> queryAll() {
        return mapper.queryAll();
    }

    @Transactional
    @Override
    public boolean batchRemove(List<Long> ids) {
        return mapper.batchRemove(ids)>0;
    }

    @Override
    public PageBean<T> queryPage(BaseQuery query) {
        //使用PageHelper设置当前页和每页显示的记录数
        PageHelper.startPage(query.getCurrentPage(),query.getPageSize());
        //调用mapper层查询
        List<T> rows = mapper.queryCondition(query);
        //构建一个pageInfo对象
        PageInfo pageInfo = new PageInfo(rows);
        //从pageInfo中获取总记录
        long total = pageInfo.getTotal();

        //构建PageBean返回
        PageBean<T> pageBean = new PageBean<>();
        pageBean.setTotals(total);
        pageBean.setRows(rows);

        return pageBean;
    }

    @Override
    public PageBean<T> pageList(BaseQuery query) {
        Long totals = mapper.queryCount(query);
        List<T> list = mapper.queryData(query);
        return new PageBean<>(totals,list);
    }


}
